package com.baiyi.cloud.platform.service.jobhandler;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;

import com.github.pagehelper.PageHelper;
import com.baiyi.cloud.platform.model.PageModel;
import com.baiyi.cloud.platform.oa.entity.SysUser;
import com.baiyi.cloud.platform.oa.mapper.SysUserMapper;
import com.baiyi.cloud.platform.uc.entity.UcUser;
import com.baiyi.cloud.platform.uc.mapper.UcUserMapper;
import com.baiyi.cloud.platform.util.EncryUtil;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.IJobHandler;
import com.xxl.job.core.handler.annotation.JobHandler;
import com.xxl.job.core.log.XxlJobLogger;


/**
 * 任务Handler示例（Bean模式）
 *
 * 开发步骤：
 * 1、继承"IJobHandler"：“com.xxl.job.core.handler.IJobHandler”；
 * 2、注册到Spring容器：添加“@Component”注解，被Spring容器扫描为Bean实例；
 * 3、注册到执行器工厂：添加“@JobHandler(value="自定义jobhandler名称")”注解，注解value值对应的是调度中心新建任务的JobHandler属性的值。
 * 4、执行日志：需要通过 "XxlJobLogger.log" 打印执行日志；
 *
 * @author xuxueli 2015-12-19 19:43:36
 */
@JobHandler(value="dataInitJobHandler")
@Component
public class DataInitJobHandler extends IJobHandler {

	@Autowired
    private SysUserMapper sysUserMapper;
	
	@Autowired
    private UcUserMapper ucUserMapper;
	
	@Autowired
	private PasswordEncoder passwordEncoder;
	
	@Override
	public ReturnT<String> execute(String param) throws Exception {
		XxlJobLogger.log("传入参数：" + param);
		Integer oneBatch = Integer.valueOf(param);
		
		synsData(oneBatch,0);
		
		return SUCCESS;
	}
	
	
	public void synsData(Integer oneBatch, Integer start) {
		
		//删除user-center sys_user表数据
		ucUserMapper.batchDelete(null);
		
		while (true) {
			PageModel<SysUser> page = new PageModel<>();
			page.setStart(start);
			page.setLength(oneBatch);
			PageModel<SysUser> pageList = findByPage(null, page);
			
			if (null != pageList.getData() && !pageList.getData().isEmpty()) {
				for (SysUser su : pageList.getData()) {
					
					System.out.println(su.getId());
					
					if (null != su.getUsername() && !"".equals(su.getUsername()) && !su.getUsername().equals("admin")) {
						
						if (6480 == su.getId()) {
							System.out.println(su);
						}
						
						UcUser uc = new UcUser();
						uc.setId(su.getId());
						uc.setUsername(su.getUsername());
						
						//oa user解密
						String oapd = EncryUtil.decode(su.getPassword());
						
						//uc user加密
						String pw = passwordEncoder.encode(oapd);
						
						/*boolean bl = passwordEncoder.matches("12121", "$2a$10$Wtw81uu43fGKw9lkOr1RAOTNWxQIZBsB3YDwc/5yDnr/yeG5x92EG");
						
						System.out.println(bl);*/
						
						uc.setPassword(pw);
						uc.setNickname(su.getName());
						uc.setHeadImgUrl(su.getImage());
						uc.setPhone(su.getMobile());
						uc.setSex(Integer.valueOf(su.getSex()));
						uc.setEnabled(1);
						uc.setType("BACKEND");
						uc.setCreateTime(su.getAddTime());
						uc.setUpdateTime(su.getUpdateTime());
						uc.setAttribute(su.getStatus());
						uc.setOrgId(su.getOrgid());
						uc.setStationId(su.getStationid());
						uc.setEmail(su.getEmail());
						uc.setIdCard(su.getIdcard());
						uc.setIsAdmin(Integer.valueOf(su.getIsAdmin()));
						uc.setOperate(su.getOperate());
						uc.setOpStatus(su.getOpStatus());
						uc.setMainRoleId(su.getMainRoleId());
						uc.setIsDivide(su.getIsDivide());
						uc.setRemoteControl(su.getRemoteControl());
						uc.setAddLineShowAllDevice(su.getAddLineShowAllDevice());
						uc.setLeaveDate(su.getLeaveDate());
						uc.setCoopTel(su.getCoopTel());
						uc.setSeeOfflineDevice(su.getSeeOfflineDevice());
						uc.setIsShowTotalIncome(su.getIsShowTotalIncome());
						uc.setIsNetReward(su.getIsNetReward());
						uc.setIsSPHeader(su.getIsSPHeader());
						uc.setWxGZHOpenId(su.getWxGZHOpenId());
						
						ucUserMapper.add(uc);
					}
					else {
						System.out.println(su);
					}
				}
			}
			start = (oneBatch + start);
			if (start > pageList.getRecordsTotal()) {
				System.out.println("同步完成");
				break;
			}
		}
	}
	
	private PageModel<SysUser> findByPage(SysUser sysUser, PageModel<SysUser> page) {
		
		PageHelper.offsetPage(page.getStart(), page.getLength());
		page.initData(sysUserMapper.findByPage(sysUser));
		
		return page;
	}


}
